-- Read the weapon_real_base if you really want to know what each action does

if (SERVER) then
	AddCSLuaFile("shared.lua")
	SWEP.HoldType 		= "shotgun"
end

if (CLIENT) then
	SWEP.PrintName 		= "BENELLI M3 SUPER 90"
	SWEP.Author 		= "WORSHIPPER"
	SWEP.Slot 			= 3
	SWEP.SlotPos 		= 1
	SWEP.IconLetter 		= "k"

	killicon.AddFont("weapon_real_cs_pumpshotgun", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ))
end

/*---------------------------------------------------------
Muzzle Effect + Shell Effect
---------------------------------------------------------*/
SWEP.MuzzleEffect			= "rg_muzzle_grenade" -- This is an extra muzzleflash effect
-- Available muzzle effects: rg_muzzle_grenade, rg_muzzle_highcal, rg_muzzle_hmg, rg_muzzle_pistol, rg_muzzle_rifle, rg_muzzle_silenced, none

SWEP.ShellEffect			= "rg_shelleject_shotgun" -- This is a shell ejection effect
-- Available shell eject effects: rg_shelleject, rg_shelleject_rifle, rg_shelleject_shotgun, none

SWEP.MuzzleAttachment		= "1" -- Should be "1" for CSS models or "muzzle" for hl2 models
SWEP.ShellEjectAttachment	= "2" -- Should be "2" for CSS models or "1" for hl2 models

SWEP.EjectDelay				= 0.53
/*-------------------------------------------------------*/

local ShotgunReloading
ShotgunReloading = false

SWEP.Instructions 		= "Damage: 35% \nRecoil: 50% \nRate of Fire: Pump-Action"

SWEP.Base = 			"weapon_real_base_pistol"

SWEP.Spawnable 			= true
SWEP.AdminSpawnable 		= true

SWEP.ViewModel 			= "models/weapons/v_shot_m3super90.mdl"
SWEP.WorldModel 			= "models/weapons/w_shot_m3super90.mdl"

SWEP.Primary.Sound 		= Sound("Weapon_M3.Single")
SWEP.Primary.Recoil 		= 5
SWEP.Primary.Damage 		= 35
SWEP.Primary.NumShots 		= 12
SWEP.Primary.Cone 		= 0.075
SWEP.Primary.ClipSize 		= 8
SWEP.Primary.Delay 		= 0.95
SWEP.Primary.DefaultClip 	= 8
SWEP.Primary.Automatic 		= false
SWEP.Primary.Ammo 		= "buckshot"

SWEP.Secondary.ClipSize 	= -1
SWEP.Secondary.DefaultClip 	= -1
SWEP.Secondary.Automatic 	= false
SWEP.Secondary.Ammo 		= "none"

SWEP.IronSightsPos 		= Vector (5.7431, -1.6786, 3.3682)
SWEP.IronSightsAng 		= Vector (0.0634, -0.0235, 0)

SWEP.data 				= {}
SWEP.mode 				= "auto"	-- Start with the buckshot rounds
SWEP.data.newclip 		= false

SWEP.data.semi 			= {}		-- Slug Rounds
SWEP.data.semi.Cone 		= 0.015	-- Cone of the slug rounds
SWEP.data.semi.NumShots 	= 1		-- 1 big lead
SWEP.data.semi.Damage 		= 90		-- Damage of the big lead

SWEP.data.auto 			= {}		-- Buckshot Rounds
SWEP.data.auto.Cone 		= 0.075	-- Cone of the buckshot rounds
SWEP.data.auto.NumShots 	= 12		-- 12 little leads
SWEP.data.auto.Damage 		= 35		-- Damage of a lead

/*---------------------------------------------------------
Auto/Semi Configuration
---------------------------------------------------------*/
function SWEP.data.semi.Init(self)
	self.Primary.Automatic = false
	self.Weapon:EmitSound("weapons/smg1/switch_single.wav")
end
function SWEP.data.auto.Init(self)
	self.Primary.Automatic = false
	self.Weapon:EmitSound("weapons/smg1/switch_burst.wav")
end

/*---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	if not self:CanPrimaryAttack() or self.Owner:WaterLevel() > 1 then return end
	-- If your gun have a problem or if you are under water, you'll not be able to fire

	self.Reloadaftershoot = CurTime() + self.Primary.Delay
	-- Set the reload after shoot to be not able to reload when firering

	self.Weapon:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
	-- Set next secondary fire after your fire delay

	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	-- Set next primary fire after your fire delay

	self.Weapon:EmitSound(self.Primary.Sound)
	-- Emit the gun sound when you fire

	if (self:GetIronsights() == true) then
		self:CSShootBullet(self.data[self.mode].Damage, 0, self.data[self.mode].NumShots, self.data[self.mode].Cone)
		-- Slug/Buckshot Rounds

		self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 2), math.Rand(-1,1) * (self.Primary.Recoil / 2), 0))
		-- Punch the screen 2x less hard when you're in ironsigh mod
	else
		self:CSShootBullet(self.data[self.mode].Damage, self.Primary.Recoil, self.data[self.mode].NumShots, self.data[self.mode].Cone)
		-- Slug/Buckshot Rounds

		self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * self.Primary.Recoil, math.Rand(-1,1) *self.Primary.Recoil, 0))
		-- Punch the screen
	end

	self:TakePrimaryAmmo(1)
	-- Take 1 ammo in you clip

	if ((SinglePlayer() and SERVER) or CLIENT) then
		self.Weapon:SetNetworkedFloat("LastShootTime", CurTime())
	end
end

/*---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	if self.Owner:KeyDown(IN_USE) then
		if self.mode == "semi" then
			self.mode = "auto"
			self.Owner:PrintMessage( HUD_PRINTCENTER, "Buckshot Rounds" )
			self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)
		else
			self.mode = "semi"
			self.Owner:PrintMessage( HUD_PRINTCENTER, "Slug Rounds" )
			self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)
		end
		self.data[self.mode].Init(self)
		
		if self.mode == "auto" then
			self.Weapon:SetNetworkedInt("csef",1)
		elseif self.mode == "semi" then
			self.Weapon:SetNetworkedInt("csef",3)
		end

	elseif SERVER then
		end
end

/*---------------------------------------------------------
Deploy
---------------------------------------------------------*/
function SWEP:Deploy()
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	-- Set the deploy animation when deploying

	self:SetIronsights( false )
	-- Set the ironsight mod to false

	self.Weapon:SetNextPrimaryFire(CurTime() + 1)
	-- Set the next primary fire to 1 second after deploying

	ShotgunReloading = false
	self.Weapon:SetNetworkedBool( "reloading", false)

	return true
end

/*---------------------------------------------------------
Reload
---------------------------------------------------------*/
function SWEP:Reload()

	if ( self.Reloadaftershoot > CurTime() ) then return end

	self:SetIronsights(false)

	if (self.Weapon:GetNWBool("reloading", false)) or ShotgunReloading then return end

	if (self.Weapon:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount(self.Primary.Ammo) > 0) then
			ShotgunReloading = true
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
			self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_START)
		timer.Simple(0.3, function()
			ShotgunReloading = false
			self.Weapon:SetNetworkedBool("reloading", true)
			self.Weapon:SetVar("reloadtimer", CurTime() + 1)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
		end)
	end
end

/*---------------------------------------------------------
Think
---------------------------------------------------------*/
function SWEP:Think()

	if self.Weapon:Clip1() > self.Primary.ClipSize then
		self.Weapon:SetClip1(self.Primary.ClipSize)
	end

	if self.Weapon:GetNetworkedBool( "reloading") == true then
	
		if self.Weapon:GetNetworkedInt( "reloadtimer") < CurTime() then
			if self.unavailable then return end

		if ( self.Weapon:Clip1() >= self.Primary.ClipSize || self.Owner:GetAmmoCount( self.Primary.Ammo ) <= 0 ) then
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			self.Weapon:SetNetworkedBool( "reloading", false)
			self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)
		else
			
		self.Weapon:SetNetworkedInt( "reloadtimer", CurTime() + 0.45 )
		self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
		self.Owner:RemoveAmmo( 1, self.Primary.Ammo, false )
		self.Weapon:SetClip1(  self.Weapon:Clip1() + 1 )
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
		self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)

			if ( self.Weapon:Clip1() >= self.Primary.ClipSize || self.Owner:GetAmmoCount( self.Primary.Ammo ) <= 0) then
				self.Weapon:SetNextPrimaryFire(CurTime() + 1.5)
				self.Weapon:SetNextSecondaryFire(CurTime() + 1.5)
			else
				self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
				self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
			end
		end
	end
end

if !self.Owner:KeyDown(IN_USE) then 

	if self.Owner:KeyPressed(IN_ATTACK2) then
		self:SetIronsights(true)
		self.Owner:SetFOV( 70, 0.15 )
		self.Owner:SetNetworkedInt("ScopeLevel", 1)
		if CLIENT then return end
 	end
end

	if self.Owner:KeyReleased(IN_ATTACK2) then
		self:SetIronsights(false)
		self.Owner:SetFOV( 0, 0.15 )
		self.Owner:SetNetworkedInt("ScopeLevel", 0)
		if CLIENT then return end
	end

	if self.Owner:KeyPressed(IN_ATTACK) and (self.Weapon:GetNWBool("reloading", true)) then
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
		self.Weapon:SetNetworkedBool( "reloading", false)
		self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)
	end
end

/*---------------------------------------------------------
ShootBullet
---------------------------------------------------------*/
function SWEP:CSShootBullet(dmg, recoil, numbul, cone)
	numbul = numbul or 1
	cone = cone or 0.01

	local PlayerAim = self.Owner:GetAimVector()
	local PlayerPos = self.Owner:GetShootPos()

	local bullet = {}
	bullet.Num  = numbul
	bullet.Src = self.Owner:GetShootPos()     -- Source
	bullet.Dir = self.Owner:GetAimVector()    -- Dir of bullet
	bullet.Spread = Vector(cone, cone, 0)     -- Aim Cone
	bullet.Tracer = 1       			-- Show a tracer on every x bullets
	bullet.Force = 0.5 * dmg     				-- Amount of force to give to phys objects
	bullet.Damage = dmg

	self.Owner:FireBullets(bullet)
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)      	-- View model animation
	self.Owner:MuzzleFlash()        -- Crappy muzzle light
	self.Owner:SetAnimation(PLAYER_ATTACK1)       			-- 3rd Person Animation

	local fx = EffectData()
	fx:SetEntity(self.Weapon)
	fx:SetOrigin(PlayerPos)
	fx:SetNormal(PlayerAim)
	fx:SetAttachment(self.MuzzleAttachment)
	util.Effect(self.MuzzleEffect,fx)						-- Additional muzzle effects
	
	timer.Simple( self.EjectDelay, function()
		local fx = EffectData()
		fx:SetEntity(self.Weapon)
		fx:SetNormal(PlayerAim)
		fx:SetAttachment(self.ShellEjectAttachment)

		util.Effect(self.ShellEffect,fx)					-- Shell ejection
	end)

	if ((SinglePlayer() and SERVER) or (not SinglePlayer() and CLIENT)) then
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles(eyeang)
	end
end